import AV from "leancloud-storage";

const appId = 'o7gkcpWvDsEywOvDS6G0QwTf-gzGzoHsz';
const appKey = 'hAsx98AMiq2d6B3yRbeeFhky';
const masterKey = 'WNpqkTm0RcDhhbmYCpfNNl9B';

AV.init({
  appId,
  appKey,
  masterKey
  // 启用美国节点
  // region: 'us',
});



export function login(userData, success, error){
  return AV.User.logIn(userData.username, userData.password);
}

export function reg(userData, success, error){
  var user = new AV.User();
  user.set(userData);
  return user.signUp();
}

export function currentUser(){
  let user = AV.User.current();
  return user;
}

export function logOut(){
  AV.User.logOut();
}

export function addOneUserByAdmin(userData){
  let User = AV.Object.extend("_User");
  let user = new User();
   user.set("username", userData.username);
   user.set("password", userData.password);
   user.set("email", userData.email);

  return user.save(null)
}

export function getUserById(id){
  let query = new AV.Query('_User');
  return query.get(id);
}

export function destoryUser(id){
  // console.log(id);
  return getUserById(id).then(function(user){
   return user.destroy();
  }, function(error){
    console.log(error);
  });
}

export function fetchUsers(condition){

   let query = new AV.Query('_User');

   query.limit(condition.limit);
   query.skip(condition.skip);

   query.descending(condition.descending);
   return query.find();
  //  .then(function(users){
  //    console.log(users);
  //  }, function(errors){
  //    console.log(errors);
  //  });
}
